var express = require("express");
var router = express.Router();

var services = require("../services");

function Result(res, result, userMsg = "", data = {}) {
  res.send(Object.assign({
    code: 0,
    data: result,
    userMsg
  },data))
}

// 确定点击报道
router.get('/sure',async function(req,res){
  var stuNo = req.query.stuNo;
  var result = await services.updateStudent(stuNo);
  Result(res,result,'请求成功')
})

router.get('/notSure',async function(req,res){
  var stuNo = req.query.stuNo;
  var result = await services.updateNot(stuNo);
  Result(res,result,'请求成功')
})

router.get('/findByStuNo' , async function (req ,res) {
  var stuNo = req.query.stuNo;
  var result = await services.findStudentByStuNo(stuNo);
  if (!result[0]) {
    res.send({
      code: 500,
      data: '未查到改学生！',
      userMsg: '很抱歉，未找到该学生'
    })
  } else {
    Result(res,result[0],'请求成功')
  }
})

// 统计新生总人数
router.get('/count', async function(req,res){
  const result = await services.count();
  Result(res,result,'请求成功')
})

// 按照学院统计新生总人数
router.get('/collegeCount' , async function(req , res){
  var college = req.query.college;
  if (college !== '云南农业大学') {
    const result = await services.collegeCount(college);
    Result(res,result,'请求成功')
    return
  } else {
    const { reportNum, studentNum } = await services.count();
    Result(res,{ 
      'college': '云南农业大学',
      reportNum,
      'collegeNum': studentNum
    }, '请求成功')
  }
})

// 所有学院的情况
router.get('/allCollege' , async function(req , res){
  const result = await services.allCollege();
  Result(res,result,'请求成功')
})

// 学院男女比例情况
router.get('/collegeSex' , async function(req,res){
  const college = req.query.college;
  const result = await services.collegeSex(college)
  Result(res,result,'请求成功')
})

// 男女生报道比例
router.get('/sexCount' , async function(req , res){
  const result = await services.sexCount()
  Result(res,result,'请求成功')
})

// 查询学生
router.get('/findStudent',async function(req, res){
  const result = await services.findStudent(req.query)
  Result(res,result,'请求成功')
})

router.get('/majorCount', async function(req, res) {
  const college = req.query.college
  const result = await services.majorCount(college)
  Result(res,result,'请求成功')
})

router.get('/findStudentByMajor', async function(req, res) {
  const result = await services.findStudentByMajor(req.query)
  Result(res,result,'请求成功')
})

router.get('/report/college', async function(req, res) {
  const college = req.query.college
  if (!college) {
    return res.send({ code: 500, data: '未查询到用户所处学院', userMsg: '请先完成易班注册认证！'})
  } else if (college !== '云南农业大学'){
    const collegeSex = await services.collegeSex(college)
    const major = await services.majorCount(college)
    const result = {
      collegeSex,
      major
    }
    Result(res,result,'请求成功')
    return
  } else {
    const { sexM, sexF, sexProportion } = await services.sexCount()
    const collegeSex = {
      'collegeNum': sexM + sexF,
      'sexNumM': sexM,
      'sexNumF': sexF,
      sexProportion
    }
    const major = {}
    const result = {
      collegeSex,
      major
    }
    Result(res,result,'请求成功')
  }
})

router.get('/majorList', async function(req, res) {
  const result = await services.majorList(req.query.college)
  Result(res,result,'请求成功')
})

module.exports = router;